package tresp.utils;

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.logging.Logger;

import tresp.utils.logging.MyLogger;



/**
 * 
 * <b>ThreadManager</b>
 * 
 * <p>
 * 	This class is responsible for thread management.
 * 
 * </p>
 * 
 * @author <font color="blue">Maxim Kirilov</font>
 *
 */
public class ThreadManager{

    
    private static Logger logger = MyLogger.getLogger( ThreadManager.class );
    
    /**class instance*/
    private static ThreadManager threadManager = null;
    
    /**Thread executor*/
    private ThreadFactory threadFactory;
    
    

    public static ThreadManager getInstance(){
	
	if ( threadManager == null ){
	    synchronized( ThreadManager.class ){
		if ( threadManager == null ){
		    threadManager = new ThreadManager();
		}
	    }
	}
	return threadManager;
    }
    
    private  ThreadManager(){
	threadFactory = Executors.defaultThreadFactory();
	logger.info("Initializing Thread Manager");
    }
    


    
    public void submit(Runnable command) {
	Thread t = threadFactory.newThread( command );
	t.start();
    }

    public void submit(String name, Runnable command) {
	Thread t = threadFactory.newThread( command );
	if ( name != null) { t.setName(name); }
	t.start();
    }

}
